
/**
 * @name MassWG (MASS Wind Gradient 1) Calculates surface moisture flux for
 * January 2-31, 1980 Part of Pacific Northwest Climate Data Analysis
 *
 * @version 0.1.504 Alpha, MASS Wind Gradient 1, 4th commit
 * @date 10/4/2013
 * @author Niko Simonson
 * @author Brett Yasutake
 */

import java.awt.Color;
import java.awt.Graphics;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import ucar.ma2.ArrayDouble;
import ucar.ma2.IndexIterator;
import ucar.nc2.NetcdfFile;
import ucar.nc2.Variable;

/**
 * @name RVPanel
 * 
 */

public class RVPanel extends javax.swing.JPanel {
    ResultsViewer parent;
    private Graphics graphics;
    private Color[] rvColor;
    
    public void paintAll(Graphics g) {
        
        
        super.paintAll(g);
        drawGraphics(parent.inFile, parent.dimensions);
    }
    
    public void startGraphics() {
        
        
        // prepare cell colors
	rvColor = new Color[21];
	rvColor[0] = new Color( 0x0000FF );   // blue
	rvColor[1] = new Color( 0x0033FF );
	rvColor[2] = new Color( 0x0066FF );
	rvColor[3] = new Color( 0x0099FF );
	rvColor[4] = new Color( 0x00CCFF );
	rvColor[5] = new Color( 0x00FFFF );
	rvColor[6] = new Color( 0x00FFCC );
	rvColor[7] = new Color( 0x00FF99 );
	rvColor[8] = new Color( 0x00FF66 );
	rvColor[9] = new Color( 0x00FF33 );
	rvColor[10] = new Color( 0x00FF00 );  // green
	rvColor[11] = new Color( 0x33FF00 );
	rvColor[12] = new Color( 0x66FF00 );
	rvColor[13] = new Color( 0x99FF00 );
	rvColor[14] = new Color( 0xCCFF00 );
	rvColor[15] = new Color( 0xFFFF00 );
	rvColor[16] = new Color( 0xFFCC00 );
	rvColor[17] = new Color( 0xFF9900 );
	rvColor[18] = new Color( 0xFF6600 );
	rvColor[19] = new Color( 0xFF3300 );
	rvColor[20] = new Color( 0xFF0000 );  // red

    }
    
    private void drawGraphics(NetcdfFile inFile, int[] dimensions) {
        Variable values;
        ArrayDouble.D2 ary;
        values = inFile.getVariables().get(0);
        double min = 0.0, max = 0.0;
        double[][] vals = new double[dimensions[0]][dimensions[1]];
        double cellWidth, cellHeight;
        int pixWidth, pixHeight;

        cellWidth = this.getWidth() / dimensions[0];
        cellHeight = this.getHeight() / dimensions[1];
        pixWidth = (int) cellWidth;
        pixHeight = (int) cellHeight;


        try {
            ary = (ArrayDouble.D2) values.read();

            int counter = 0;
            int x = 0, y = 0;
            IndexIterator it = ary.getIndexIterator();

            while (it.hasNext()) {
                vals[x][y] = (double) it.next();
                if (vals[x][y] > max) {
                    max = vals[x][y];
                }

                if (vals[x][y] < min) {
                    min = vals[x][y];
                }

                ++x;
                if (x == dimensions[0]) {
                    x = 0;
                    ++y;
                }




            } // end while
            double absRange = max - min;
            double ratio;
            double adjustedVal;
            int index;
            if (absRange == 0) {
                System.err.println("No variation in values");
            } else {

                this.setVisible(true);
                System.err.println("About to draw");
                for (int i = 0; i < dimensions[0]; ++i) {
                    for (int j = 0; j < dimensions[1]; ++j) {
                        adjustedVal = vals[i][j] - min;

                        ratio = (adjustedVal / absRange) * 20;

                        index = (int) ratio;

                        graphics.setColor(rvColor[index]);
                        graphics.fill3DRect(x * pixWidth, y * pixHeight, pixWidth, pixHeight, true);
                        
                        

                    }
                    System.err.println("Finished a row");
                    
                }
                
                graphics.setColor(rvColor[10]);
              graphics.fill3DRect(0, 0, 100, 100, true);
              graphics.fill3DRect(500, 500, 500, 500, true);
              graphics.fillRect(0, 0, 100, 100);
              graphics.fillRect(500, 500, 500, 500);
              graphics.fill3DRect(0, 0, 100, 100, true);
//                rvPanel.paintComponents(graphics);
//                rvPanel.repaint();
                System.err.println("Finished drawing");
                

            }


        } catch (IOException ex) {
            Logger.getLogger(ResultsViewer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Creates new form RVPanel
     */
    public RVPanel(ResultsViewer par) {
        initComponents();
        parent = par;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
